<template>
  <div class="department-container">
    <!-- 顶部导航栏 -->
    <div class="header">
      <div class="header-content">
        <el-button @click="goBack" type="text" class="back-btn">
          <el-icon><ArrowLeft /></el-icon>
          返回
        </el-button>
        <h2>科室查询</h2>
        <div></div>
      </div>
    </div>

    <!-- 搜索栏 -->
    <div class="search-section">
      <el-input
        v-model="searchKeyword"
        placeholder="搜索科室的名称"
        prefix-icon="Search"
        clearable
        @input="handleSearch"
      />
    </div>

    <!-- 科室的列表 -->
    <div class="department-list">
      <div 
        v-for="department in filteredDepartments" 
        :key="department.id"
        class="department-item"
        @click="goToDoctors(department.id)"
      >
        <div class="department-icon">{{ department.icon }}</div>
        <div class="department-info">
          <h3>{{ department.name }}</h3>
          <p>{{ department.description }}</p>
        </div>
        <div class="department-arrow">
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading">
      <el-skeleton :rows="3" animated />
    </div>

    <!-- 空状态 -->
    <div v-if="!loading && filteredDepartments.length === 0" class="empty-state">
      <el-empty description="暂无相关科室" />
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { mockAPI } from '../utils/mockData'

export default {
  name: 'DepartmentList',
  setup() {
    const router = useRouter()
    const departments = ref([])
    const filteredDepartments = ref([])
    const searchKeyword = ref('')
    const loading = ref(false)

    // 获取科室列表
    const loadDepartments = async () => {
      loading.value = true
      try {
        const result = await mockAPI.getDepartments()
        if (result.success) {
          departments.value = result.data
          filteredDepartments.value = result.data
        }
      } catch (error) {
        ElMessage.error('获取科室列表失败')
        console.error('获取科室列表失败:', error)
      } finally {
        loading.value = false
      }
    }

    // 搜索功能
    const handleSearch = () => {
      if (!searchKeyword.value) {
        filteredDepartments.value = departments.value
      } else {
        filteredDepartments.value = departments.value.filter(dept => 
          dept.name.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
          dept.description.toLowerCase().includes(searchKeyword.value.toLowerCase())
        )
      }
    }

    // 跳转到医生列表
    const goToDoctors = (departmentId) => {
      router.push(`/doctors/${departmentId}`)
    }

    // 返回上一页
    const goBack = () => {
      router.go(-1)
    }

    onMounted(() => {
      loadDepartments()
    })

    return {
      departments,
      filteredDepartments,
      searchKeyword,
      loading,
      handleSearch,
      goToDoctors,
      goBack
    }
  }
}
</script>

<style scoped>
.department-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 15px 0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.back-btn {
  color: white;
  font-size: 16px;
  display: flex;
  align-items: center;
}

.back-btn .el-icon {
  margin-right: 5px;
}

.header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: bold;
}

.search-section {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
}

.department-list {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px 20px;
}

.department-item {
  background: white;
  border-radius: 15px;
  padding: 20px;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.department-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.department-icon {
  font-size: 40px;
  margin-right: 20px;
  width: 60px;
  text-align: center;
}

.department-info {
  flex: 1;
}

.department-info h3 {
  margin: 0 0 8px 0;
  font-size: 18px;
  color: #333;
  font-weight: 600;
}

.department-info p {
  margin: 0;
  color: #666;
  font-size: 14px;
  line-height: 1.5;
}

.department-arrow {
  color: #999;
  font-size: 20px;
}

.loading {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
}

.empty-state {
  max-width: 1200px;
  margin: 40px auto;
  padding: 0 20px;
  text-align: center;
}

@media (max-width: 768px) {
  .department-item {
    padding: 15px;
  }
  
  .department-icon {
    font-size: 30px;
    margin-right: 15px;
    width: 50px;
  }
  
  .department-info h3 {
    font-size: 16px;
  }
  
  .department-info p {
    font-size: 13px;
  }
}
</style> 